var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// mongoose.connect("mongodb://localhost:27020/makeblock-iot");

var Devices = new Schema({
	userkey:String,
	deviceimei:String,
	devicetype: String
}/*,{ minimize: false }*/);

//CRUD Operations
//Insert
/**
 * example:
 * var userid 	  = {userid: 1};
 * var update     = {$set : {userkey : "linyanwen"}};
 * var options    = {upsert : true};
 * var callback   = function(error);
 */
Devices.methods.Insert = function(userkey, update, option, callback) {
	return this.model('makeblockdevices').update(userkey, update, option, callback);
}

//Delete
/**
 * example:
 * var userid 	  = {userid: 1};
 * var callback   = function(error);
 */
Devices.methods.Delete = function(userkey, callback) {
	return this.model('makeblockdevices').remove(userkey, callback);
}

//Update
/**
 * example:
 * var userid 	  = {userid: 1};
 * var update     = {$set : {userkey : "linyanwen"}};
 * var options    = {upsert : true};
 * var callback   = function(error);
 */
Devices.methods.Update = function(userkey, update, option, callback) {
	return this.model('makeblockdevices').update(userkey, update, option, callback);
}

//Query
/**
 * example:
 * var userid 	  = {userid: 1};
 * var callback   = function(error);
 */
Devices.methods.Query = function(userkey, callback) {
	return this.model('makeblockdevices').findOne(userkey, callback);
}


//Static method
Devices.statics.Insert = function(userkey, update, option, callback) {
	return this.model('makeblockdevices').update(userkey, update, option, callback);
}
Devices.statics.Delete = function(userkey, callback) {
	return this.model('makeblockdevices').remove(userkey, callback);
}
Devices.statics.Update = function(userkey, update, option, callback) {
	return this.model('makeblockdevices').update(userkey, update, option, callback);
}
Devices.statics.Query = function(userkey, callback) {
	return this.model('makeblockdevices').findOne(userkey, callback);
}

exports.Devices=mongoose.model('makeblockdevices', Devices);
